<?php
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';

class AccountController extends Zend_Controller_Action
{
	protected $_redirectUrl = '/';
	
	protected $_application;
	
	
	
	
    public function init()
    {
    	$this->view->headTitle("Account");
    	require_once 'Zend/Session/Namespace.php';
    	$this->_application = new Zend_Session_Namespace('foodies');
    }

    public function indexAction()
    {
    	if(Zend_Auth::getInstance()->hasIdentity())
    	{
    		$this->_redirect('account/profile');
    	}
    	else 
    	{
    		$this->_redirect('account/login');
    	}
    	
    	
    }
	
    public function registerAction()
    {
    	$form = new Form_Register();
    	$this->view->form = $form;
    		
    }
    
	public function loginAction(){
		$form = new Form_Login();
		$this->view->form = $form;
	}
 

   
	public function successAction()
	{
		$form = new Form_Register();
	
		if($form->isValid($_POST))
		{
			$email = $form->getValue("email");
			$username = $form->getValue("username");
			$password = $form->getValue("password");
			$firstname = $form->getValue("firstname");
			$lastname = $form->getValue("lastname");
			$gender=$form->getValue("gender");
			$phone=$form->getValue("phone");
			$dateofbirth=$form->getValue("dateofbirth");
			$address=$form->getValue("address");
			$acc=new Model_Account();
			$acc->setUsername($username)->setPassword($password)->setEmail($email)->setFirstName($firstname)->setLastName($lastname)->setGender($gender)->setDateOfBirth($dateofbirth)->setPhone($phone);
			$accMapper=new Model_AccountMapper();
			$accMapper->createNewAccount($acc,$address);
			$this->logmeAction($username, $password);
			
		}else{
			$this->view->errors = $form->getMessages();
			$this->view->form = $form;
		}
		
	}
	
	public function logmeAction($username, $password){
	
	
		$request 	= $this->getRequest();
		$registry 	= Zend_Registry::getInstance();
		$auth		= Zend_Auth::getInstance();
	
		$DB = $registry['my_db'];
	
		$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
		$authAdapter->setTableName('account')
		->setIdentityColumn('Username')
		->setCredentialColumn('Password');
		
		
		// Set the input credential values
		$uname = $username;
		$paswd = $password;
		$authAdapter->setIdentity($uname);
		$authAdapter->setCredential($paswd);
	
		// Perform the authentication query, saving the result
		$result = $auth->authenticate($authAdapter);
	
		if($result->isValid()){
			$data = $authAdapter->getResultRowObject(null,'password');
			$auth->getStorage()->write($data->Username);
			//$auth->getStorage()->write($data);
			$this->_redirect('/index/index');
			
		}else{
			$this->_redirect('/account/login');
		}
	}
	
	public function authAction(){
		
		
		$request 	= $this->getRequest();
		$registry 	= Zend_Registry::getInstance();
		$auth		= Zend_Auth::getInstance();
		
		$DB = $registry['my_db'];
		
		$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
		$authAdapter->setTableName('account')
		->setIdentityColumn('Username')
		->setCredentialColumn('Password');
		
		// Set the input credential values
		$uname = $request->getParam('username');
		$paswd = $request->getParam('password');
		$authAdapter->setIdentity($uname);
		$authAdapter->setCredential($paswd);
		
		// Perform the authentication query, saving the result
		$result = $auth->authenticate($authAdapter);
		
		if($result->isValid()){
			$data = $authAdapter->getResultRowObject(null,'password');
			$auth->getStorage()->write($data->Username);
			$this->_redirect('/index/index');
		}else{
			$this->_redirect('/account/login');
		}
	}

	public function profileAction(){
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity())
		{
			$user = $auth->getIdentity();
			$username=$user;
			$accM=new Model_AccountMapper();
			$acc=$accM->accountDetail($username);
			$this->view->username=$acc->getUsername();
			$this->view->email=$acc->getEmail();
			$this->view->gender=$acc->getGender();
			$this->view->firstname=$acc->getFirstName();
			$this->view->lastname=$acc->getLastName();
			$this->view->date=$acc->getDateOfBirth();
			$this->view->phone=$acc->getPhone();
			$this->view->image=$acc->getImage();
			
		}
	}
	
	public function logoutAction()
	
	{
	
		$auth = Zend_Auth::getInstance();
	
		$auth->clearIdentity();
	
		$this->_redirect('/index');
	
	}
	
	public function editprocessAction()
	{
		$username=$this->_getParam("acc-username");
		$email=$this->_getParam("acc-email");
		$password=$this->_getParam("acc-pass");
		$passwordConfirm=$this->_getParam("acc-passconfirm");
		$firstname=$this->_getParam("firstname");
		$lastname=$this->_getParam("lasttname");
		$gender=$this->_getParam("gender");
		$dateofbirth=$this->_getParam("DateOfBirth");
		$phone=$this->_getParam("Phone");
		
		$error="";
		
		$account=new Model_Account();
		$account->setDateOfBirth($dateofbirth)->setEmail($email)->setFirstName($firstname)->setGender($gender)->setLastName($lastname)->setPassword($password)->setPhone($phone)->setUsername($username);
		
		$accM=new Model_AccountMapper();
		$result=$accM->updateAccount($account);
		
		$this->_redirect('/account/profile');
		
	}
	
}
    
    


